package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张伟奇
 * @Date 2021/7/17 17:38
 * @description
 * @Version 1.0
 **/
@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecParamService specParamService;
    @Autowired
    private SpecGroupService specGroupService;

    /**
     * 根据分类ID查询，查询规格组的集合
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>>  listSpecGroupByCategory(@RequestParam("id") Long cid){

        return  ResponseEntity.ok(specGroupService.listSpecGroupByCategory(cid));
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */

    @GetMapping("/params")
    public  ResponseEntity<List<SpecParamDTO>> listSpecParamByCategoryId(
                    @RequestParam(value = "categoryId",required = false) Long categoryId,
                    @RequestParam(value = "groupId",required = false) Long groupId,
                    @RequestParam(value = "searching",required = false) Boolean searching
                    ){
        return ResponseEntity.ok(specParamService.listSpecParamByCategoryId(categoryId,groupId,searching));
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */

    @PostMapping("/group")
    public  ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
            this.specGroupService.saveGroupSaveGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */

    @PutMapping("/group")
    public  ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.updateGroupSaveGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */

    @PostMapping("/param")
    public  ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.saveSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public  ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类查询id查询规格组内参数
     * @param id
     * @return
     */
    @GetMapping("/list")
    public  ResponseEntity<List<SpecGroupDTO>> listSpecGroupWithParams(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.specGroupService.listSpecGroupWithParams(id));
    }


}
